package com.keydom.ropeskipping.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.Objects;

/**
 * @author Jordan
 * @date 2021/1/26 11:23
 */
public class BluetoothListenerReceiver extends BroadcastReceiver {
    private OnReceiveCallbackListener onReceiveCallbackListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(Objects.requireNonNull(intent.getAction()))) {
            int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
            onReceiveCallbackListener.onReceiveCallback(blueState);
            switch (blueState) {
                case BluetoothAdapter.STATE_TURNING_ON:
                    Log.e("蓝牙", "onReceive---------蓝牙正在打开中");
                    break;
                case BluetoothAdapter.STATE_ON:
                    Log.e("蓝牙", "onReceive---------蓝牙已经打开");
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    Log.e("蓝牙", "onReceive---------蓝牙正在关闭中");
                    break;
                case BluetoothAdapter.STATE_OFF:
                    Log.e("蓝牙", "onReceive---------蓝牙已经关闭");
                    break;
                default:
                    break;
            }
        }
    }

    public void setOnReceiveCallbackListener(OnReceiveCallbackListener mOnReceiveCallbackListener) {
        this.onReceiveCallbackListener = mOnReceiveCallbackListener;
    }

    public interface OnReceiveCallbackListener {
        /**
         * 连接回调
         * @param blueState 连接状态
         */
        void onReceiveCallback(int blueState);
    }
}
